package com.alks.common.utils.beanutils;

import com.alks.common.utils.stringUtils.CharsetKit;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * @author MS
 * @since 2024/6/7下午3:11
 */
public class StringUtils {
    /**
     * 将小驼峰转化为大写下划线格式（数据库字段）
     *
     * @param input 待转换数据
     * @return 转换后数据
     */
    public static String convertToUpper(String input) {
        StringBuilder result = new StringBuilder();
        boolean isFirstChar = true;

        for (char c : input.toCharArray()) {
            if (Character.isUpperCase(c) && !isFirstChar) {
                result.append("_");
            }
            result.append(Character.toUpperCase(c));
            isFirstChar = false;
        }

        return result.toString();
    }

    public static String format(String strPattern, Object... argArray) {
        if (!StringUtils.isEmpty(strPattern) && !StringUtils.isAllEmpty(argArray)) {
            int strPatternLength = strPattern.length();
            StringBuilder sbuf = new StringBuilder(strPatternLength + 50);
            int handledPosition = 0;

            for (int argIndex = 0; argIndex < argArray.length; ++argIndex) {
                int delimIndex = strPattern.indexOf("{}", handledPosition);
                if (delimIndex == -1) {
                    if (handledPosition == 0) {
                        return strPattern;
                    }

                    sbuf.append(strPattern, handledPosition, strPatternLength);
                    return sbuf.toString();
                }

                if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == '\\') {
                    if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == '\\') {
                        sbuf.append(strPattern, handledPosition, delimIndex - 1);
                        sbuf.append(utf8Str(argArray[argIndex]));
                        handledPosition = delimIndex + 2;
                    } else {
                        --argIndex;
                        sbuf.append(strPattern, handledPosition, delimIndex - 1);
                        sbuf.append('{');
                        handledPosition = delimIndex + 1;
                    }
                } else {
                    sbuf.append(strPattern, handledPosition, delimIndex);
                    sbuf.append(utf8Str(argArray[argIndex]));
                    handledPosition = delimIndex + 2;
                }
            }

            sbuf.append(strPattern, handledPosition, strPattern.length());
            return sbuf.toString();
        } else {
            return strPattern;
        }
    }

    public static String utf8Str(Object obj) {
        return str(obj, CharsetKit.CHARSET_UTF_8);
    }

    public static String str(Object obj, Charset charset) {
        if (null == obj) {
            return null;
        } else if (obj instanceof String) {
            return (String)obj;
        } else if (!(obj instanceof byte[]) && !(obj instanceof Byte[])) {
            return obj instanceof ByteBuffer ? str((ByteBuffer)obj, charset) : obj.toString();
        } else {
            return str((Object)((Byte[])((Byte[])obj)), (Charset)charset);
        }
    }

    public static boolean isNull(Object obj) {
        return obj == null;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }

    public static boolean isAllEmpty(Object[] objects) {
        return isNull(objects) || objects.length == 0;
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
}
